#include "init.c"
#include "util.c"
#include "detail.c"
#include "getMail.c"
#include "search.c"


#define MAXLINE 8192

#define MENU "\n\nmypop > You Can:\n\n\t [ 1 ] Get a list of messages and sizes      \
            \n\t [ 2 ] Get mail status\
            \n\t [ 3 ] Display mail in detail\n\t [ 4 ] Search text in local mails\
            \n\t [ 5 ] Display by subjects\n\t [ 6 ] Download the mail and delete in the remote service\
            \n\t [ 7 ] Quit\n\nmypop > Your option: "
#define bzero(ptr, n) memset(ptr, 0, n);
#define SA struct sockaddr
int errorPass = 0;

void option(int sock);

int main(int argc, char const *argv[])
{

    int sock = connectPOP();
    //用户名+密码，测试时省去输入及验证
    // char username[] = "youyang3333@126.com";
    // char password[] = "TJONKOKTRMZBRQHY";
    char username[50], password[50];
    do
    {
        bzero(username, 50);
        printf("mypop > Your username: ");
        scanf("%s", username);
        CLEAR_BUF
        bzero(password, 50);
        printf("mypop > Your password: ");
        scanf("%s", password);
        CLEAR_BUF
    } while (!login(username, password, sock));
    // login(username, password, sock);
    option(sock);
    close(sock);
    return 0;
}
void option(int sock)
{
    int option = 0;
    while (1)
    {
        printf(MENU);
        scanf("%d", &option);
        CLEAR_BUF
        switch (option)
        {
        case 1:
            listMailVar(1, sock);
            break;
        case 2:
        {
            int choose = 0;
            do
            {
                printf("mypop > Please enter the number of the message you want to view status: ");
                scanf("%d", &choose);
                CLEAR_BUF
            } while (listMailVar(2, sock, choose));

            break;
        }
        case 3:
        {
            getMail(sock);
            // 做了一半，这是个简单的直接调用命令拿到具体邮件的全部内容的函数
            break;
        }
        case 4:
        {
            char path[] = "../mail/";
            char content[108];
            printf("\nmypop > Which word to search: ");
            scanf("%s", content);
            CLEAR_BUF
            printf("\nCan be found in :\n");
            fflush(stdout);
            //遍历文件
            myfilerecursive(path, content);

            break;
        }

        case 5:
        {
            int n = listNum(sock);
            printf("mypop > Total: %d\n--------------------------\n", n);
            fflush(stdout);
            int i;
            for (i = 1; i <= n; i++)
            {
                char *rec5 = divMail(getDetail(sock, i), 1);
                char *token = strtok(rec5, "\n");

                while ((token = strtok(NULL, "\n")) != NULL)
                {
                    // read headers
                    if (strncmp(token, "Subject", strlen("Subject")) == 0)
                    {
                        printf("%d:\t", i);
                        printf("%s\n", token);
                    }
                }

                // for (size_t j = 0; j < 6; j++)
                // {
                //     token = strtok(NULL, "\n");
                // }
                // printf("%d:\t", i);
                // printf("%s\n", token);
                bzero(rec5, strlen(rec5));
                //bzero(token, strlen(token));
            }
            break;
        }
        case 6:
        {
            if (download(sock) == 1)
            {
                perror("Error happened, back to the main interface.");
            }

            break;
        }

        case 7:
            send(sock, "QUIT\x0d\x0a", strlen("QUIT\x0d\x0a"), 0);
            printf("\nmypop > You are going to exit the program.\n\n");
            close(sock);
            printf("\nThank you for your use. Looking forward to seeing you again.\n\n");
            exit(0);
            break;

        default:
            printf("\nerror");
        }
    }
}
